package com.cn.lp.export.server.base.utils;

import com.cn.lp.export.server.base.serializer.SerializerContext;
import com.cn.lp.export.server.base.serializer.SerializerException;

/**
 * 对象工具类
 * Created by qirong on 2020/10/18.
 */
public class ObjectAide {

    /**
     * 对象转byte
     *
     * @param o
     * @return
     */
    public static byte[] objectToBlob(Object o) {
        try {
            return SerializerContext.getInstance().serialize(o);
        } catch (SerializerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * byte 转对象 , 对象类型无需对应，对应属性填充，没有则默认
     *
     * @param blob
     * @param type
     * @param <T>
     * @return
     */
    public static <T> T blobToObject(byte[] blob, Class<T> type) {
        try {
            return SerializerContext.getInstance().deSerialize(blob, type);
        } catch (SerializerException e) {
            throw new IllegalStateException(e);
        }
    }

}
